package com.zhuziym.db;

import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogLevel;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;

import com.zhuziym.utils.KettleUtils;

/**
 * @Title: JobMail.java
 * @Package com.zhuziym.db
 * @Description: TODO(带参数测试 JOB)
 * @author 作者 grq
 * @version 创建时间：2018年11月3日 下午1:49:09
 *
 */
public class JobMail {
	public static void main(String[] args) throws KettleException {

		KettleDatabaseRepository repository = KettleUtils.kettleDbRepository;
		String job_path = "/201811_JOB";
		String job_name = "A_邮件job_para";
		execJob(repository, job_path, job_name);
	}

	/**
	 * 执行 JOB
	 * 
	 * @param repository
	 *            kettle数据库资源库
	 * @param job_path
	 *            JOB 目录
	 * @param job_name
	 *            JOB 名称
	 * @throws KettleException
	 */
	private static void execJob(KettleDatabaseRepository repository, String job_path, String job_name) throws KettleException {
		RepositoryDirectoryInterface dir = repository.findDirectory(job_path);
		JobMeta jobMeta = repository.loadJob(repository.getJobId(job_name, dir), null);
		Job job = new Job(repository, jobMeta);
		// 设置参数
		jobMeta.setParameterValue("sql_file", "d://12");
		jobMeta.setParameterValue("sql_file_para", "java中参数");
		job.setLogLevel(LogLevel.DEBUG);
		// 设置变量 java代码中变量会覆盖kettle里面设置的变量
		job.setVariable("sql_file", "d://13");
		// 启动执行指定的job
		job.start();
		job.waitUntilFinished();// 等待job执行完；
		job.setFinished(true);
		System.out.println(job.getResult());

	}
}
